import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/checkpoint/checkpoint_prepare_game_center_add.dart';
import 'package:flutter_eve_plugin/constants/font_family.dart';
import 'package:flutter_eve_plugin/constants/font_weight.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';

class Toast {
  static OverlayEntry _overlayEntry;
  static bool _showing = false;
  static DateTime _startedTime;
  static int duration = 3000;
  static String _msg;

  static void show(
    BuildContext context,
    String msg,
  ) async {
    assert(msg != null);
    _msg = msg;
    _startedTime = DateTime.now();
    OverlayState overlayState = Overlay.of(context);
    _showing = true;
    if (_overlayEntry == null) {
      _overlayEntry = OverlayEntry(
          builder: (BuildContext context) => Positioned(
                top: MediaQuery.of(context).size.height * 2 / 3,
                child: Container(
                    padding: EdgeInsets.all(80),
                    alignment: Alignment.center,
                    width: MediaQuery.of(context).size.width,
                    child: Container(
                      decoration: BoxDecoration(
                          color: Colors.black54,
                          borderRadius: BorderRadius.all(Radius.circular(20))),
                      padding: EdgeInsets.only(
                          left: 15, right: 15, top: 5, bottom: 5),
                      child: AnimatedOpacity(
                        opacity: _showing ? 1.0 : 0.0,
                        duration: _showing
                            ? Duration(milliseconds: 100)
                            : Duration(milliseconds: 400),
                        child: Text(
                          _msg,
                          style: TextStyle(
                            fontSize: 12.0,
                            decoration: TextDecoration.none,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    )),
              ));
      overlayState.insert(_overlayEntry);
    } else {
      _overlayEntry.markNeedsBuild();
    }
    await Future.delayed(Duration(milliseconds: duration)); //等待3秒

    if (DateTime.now().difference(_startedTime).inMilliseconds >= duration) {
      _showing = false;
      _overlayEntry.markNeedsBuild();
      _overlayEntry.remove();
      _overlayEntry = null;
    }
  }

  static void showYX(
    BuildContext context,
    String msg,
  ) async {
    assert(msg != null);
    _msg = msg;
    _startedTime = DateTime.now();
    OverlayState overlayState = Overlay.of(context);
    _showing = true;
    if (_overlayEntry == null) {
      _overlayEntry = OverlayEntry(
          builder: (BuildContext context) => Center(
                child: Container(
                    width: setRatio(201),
                    child: Container(
                      decoration: BoxDecoration(
                          gradient: LinearGradient(
                              colors: [
                                Color(0xFF4C4F5D),
                                Color(0xFF757C90),
                              ],
                              begin: Alignment.topCenter,
                              end: Alignment.bottomCenter),
                          border: Border.all(
                              color: Color(0xFFB6BED7), width: setRatio(1)),
                          borderRadius: BorderRadius.all(Radius.circular(5))),
                      padding: EdgeInsets.only(
                          left: setRatio(19),
                          right: setRatio(19),
                          top: setRatio(11),
                          bottom: setRatio(11)),
                      child: AnimatedOpacity(
                        opacity: _showing ? 1.0 : 0.0,
                        duration: _showing
                            ? Duration(milliseconds: 100)
                            : Duration(milliseconds: 400),
                        child: Text(
                          _msg,
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            letterSpacing: 1,
                            fontSize: setRatio(14),
                            fontFamily: FontFamily.PINGFANG,
                            fontWeight: FontWeightCustom.PF_Medium,
                            decoration: TextDecoration.none,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    )),
              ));
      overlayState.insert(_overlayEntry);
    } else {
      _overlayEntry.markNeedsBuild();
    }
    await Future.delayed(Duration(milliseconds: duration)); //等待3秒

    if (DateTime.now().difference(_startedTime).inMilliseconds >= duration) {
      _showing = false;
      _overlayEntry.markNeedsBuild();
      _overlayEntry.remove();
      _overlayEntry = null;
    }
  }

  static void showAttr(
      BuildContext context, List<ShowAttrInfo> attrData) async {
    _startedTime = DateTime.now();
    OverlayState overlayState = Overlay.of(context);
    _showing = true;
    if (_overlayEntry == null) {
      _overlayEntry = OverlayEntry(builder: (BuildContext context) {
        var first = attrData?.first;
        List<ShowAttrInfo> list = [];
        if ((attrData?.length ?? 0) > 1) {
          list = attrData?.sublist(1);
        }

        return SafeArea(
          child: Center(
            child: Container(
                transform: Matrix4.translationValues(0, setRatio(-100), 0),
                width: setRatio(345),
                height: setRatio(100),
                decoration: BoxDecoration(
                    gradient: LinearGradient(colors: [
                      Color(0xFF4C4F5D),
                      Color(0xFF757C90),
                    ], begin: Alignment.topCenter, end: Alignment.bottomCenter),
                    border: Border.all(
                        color: Color(0xFFB6BED7), width: setRatio(1)),
                    borderRadius: BorderRadius.circular(setRatio(5))),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SizedBox(
                      height: setRatio(46),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Container(
                            width: setRatio(90),
                            height: setRatio(20),
                            margin: EdgeInsets.only(left: setRatio(16)),
                            decoration: BoxDecoration(
                                borderRadius:
                                    BorderRadius.circular(setRatio(10)),
                                color: Color(0x4F000000)),
                            child: Center(
                              child: Text.rich(TextSpan(
                                  style: TextStyle(
                                      decoration: TextDecoration.none,
                                      color: Color(0xFFFFFFFF),
                                      fontFamily: FontFamily.PINGFANG,
                                      fontWeight: FontWeightCustom.PF_Medium,
                                      fontSize: setTextSp(10)),
                                  children: [
                                    TextSpan(text: first?.title ?? ''),
                                    TextSpan(
                                        text: '${first?.num ?? 0}',
                                        style: TextStyle(
                                            color: Color(0xFFFFBF13),
                                            fontSize: setTextSp(12))),
                                  ])),
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      height: setRatio(50),
                      width: setRatio(345),
                      child: GridView.builder(
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 5, childAspectRatio: 69 / 25),
                        itemCount: list?.length ?? 0,
                        itemBuilder: (context, index) {
                          var item = list[index];
                          return Container(
                            color: index < 5
                                ? Color(0x1F000000)
                                : Colors.transparent,
                            child: Center(
                              child: Text.rich(
                                TextSpan(
                                    style: TextStyle(
                                        decoration: TextDecoration.none,
                                        color: Color(0xFFFFFFFF),
                                        fontFamily: FontFamily.PINGFANG,
                                        fontWeight: FontWeightCustom.PF_Medium,
                                        fontSize: setTextSp(10)),
                                    children: [
                                      TextSpan(text: item?.title ?? ''),
                                      TextSpan(
                                          text: ' ${item?.num ?? 0}',
                                          style: TextStyle(
                                              color: item.color ??
                                                  Color(0xFFFF9595),
                                              fontSize: setTextSp(11))),
                                    ]),
                                textAlign: TextAlign.center,
                              ),
                            ),
                          );
                        },
                      ),
                    )
                  ],
                )),
          ),
        );
      });
      overlayState.insert(_overlayEntry);
    } else {
      _overlayEntry.markNeedsBuild();
    }
  }

  static void dissAttr() async {
    _showing = false;
    _overlayEntry.markNeedsBuild();
    _overlayEntry.remove();
    _overlayEntry = null;
  }
}
